package hot_100;

public class SearchMatrix {
    public static void main(String[] args) {
        int[][] nums = new int[][]{
                {1, 4, 7, 11, 15},
                {2, 5, 8, 12, 19,},
                {3, 6, 9, 16, 22},
                {10, 13, 14, 17, 24},
                {18, 21, 23, 26, 30}};
        int target = 5;
        System.out.println(searchMatrix(nums, target));
    }

    //二分查找解搜索矩阵
    public static boolean searchMatrix(int[][] matrix, int target) {
        int i = matrix.length - 1;
        int j = 0;
        while (i >= 0 && j <= matrix[0].length - 1) {
            int t = matrix[i][j];
            if (t > target) {
                i--;
            } else if (t < target) {
                j++;
            } else {
                return true;
            }
        }
        return false;
    }
}
